package red.sonph.util.backup;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Date;
import java.util.Vector;

public class FileTool
{  
  public static byte[] readFile(String filename) throws IOException {
  byte[] buffer = (byte[])null;
  FileInputStream fin = new FileInputStream(filename);
  buffer = new byte[fin.available()];
  fin.read(buffer);
  return buffer;
  }

  public static void saveToFile(String output, String filename, Boolean isError) {
    try {   
      Date now=new Date();       
  	  //tao file theo ngay, h thang nam
  	  	String url=createFolder(now, isError);        	  
  	  	File f=new File (url+"\\"+filename);
  	  	FileOutputStream out = new FileOutputStream(f, true);      
        PrintStream ps=new PrintStream(out);
        ps.println(output);
        ps.close();      
    } catch (Exception e) {
    System.out.println(e.getMessage());
    }
  }

  public static void saveToSuccessFile(String output, String filename) {
      try {
      File f = new File("log\\success\\"+filename);
      FileOutputStream out = new FileOutputStream(f, true);      
      PrintStream ps=new PrintStream(out);
      ps.println(output);
      ps.close();
      } catch (Exception e) {
      System.out.println(e.getMessage());
      }
    }
  
  
  
  public static String createFolder(Date now, boolean isError){
		String url="";
		if (!isError){
			try {
		          String year=String.valueOf(now.getYear()+1900);
		       	  String month=String.valueOf(now.getMonth()+1);
		       	  String date=String.valueOf(now.getDate());    		       
		       	  File f1=new File ("log\\success\\"+year);
		       	  f1.mkdir();
		          File f2=new File ("log\\success\\"+year+"\\"+month);
		       	  f2.mkdir();
		       	  File f3=new File ("log\\success\\"+year+"\\"+month+"\\"+date);
		          f3.mkdir();
		          url="log\\success\\"+year+"\\"+month+"\\"+date;
		         }
		        catch (Exception e){
		        	e.printStackTrace();
		        }
		}
		else {
			try {
		          String year=String.valueOf(now.getYear()+1900);
		       	  String month=String.valueOf(now.getMonth()+1);
		       	  String date=String.valueOf(now.getDate());		       	  
		       	  File f1=new File ("log\\Exception\\"+year);
		       	  f1.mkdir();
		          File f2=new File ("log\\Exception\\"+year+"\\"+month);
		       	  f2.mkdir();
		       	  File f3=new File ("log\\Exception\\"+year+"\\"+month+"\\"+date);
		          f3.mkdir();
		          url="log\\Exception\\"+year+"\\"+month+"\\"+date;
		       	  
		         }
		        catch (Exception e){
		        	e.printStackTrace();
		        }
		}
		return url;
		
	}
  
}



